using System;
using System.Web.UI.WebControls;

using Framework.Library;
using Framework.Library.Components.Security;
using Framework.Library.Components.Tabs;
using Framework.Library.Utilities;
using Framework.Library.Components.Portal;
using Framework.Module.CMS.Library.Definitions;
using System.Collections;
using System.IO;
using Framework.Module.CMS.Library.Zones;
using Framework.Library.Components.Commands;

namespace Portal.Administrator.Portals
{
	public partial class PortalSetting : AuthenticatedControl
	{
        private ArrayList lstFoder = new ArrayList();
        private void GetFolders(string rootPath, string path)
        {
            string[] folders = Directory.GetDirectories(path);            
            for(int i=0; i<folders.Length; i++)
            {
                string value = folders[i].ToString().Substring(folders[i].ToString().IndexOf("\\Style")).Replace("\\","/");
                lstFoder.Add(new ListItem(value, value + "/Default.css"));
            }
        }

        protected void Page_Load(object sender, EventArgs e)
		{
            if ((!CurrentUser.IsSuperAdmin && CommandController.IsSystemModule(AppEnv.CommandID())) || !CommandController.IsAvailableForUser(AppEnv.CommandID(), CurrentUser.UserID, Convert.ToInt32(AppEnv.PortalId())))
                Response.Redirect(AppEnv.ADMIN_CMD, true);
			if(!Page.IsPostBack)
			{
                //Fill portals

                dropPortal.Items.Clear();
                PortalController.LoadPortals(dropPortal.Items);
                
                dropPortal.Items.Insert(0, new ListItem("Root", "0"));
                dropPortal.SelectedValue = ConvertUtility.ToString(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Home_Portal]);

                txtContactMail.Text = ConvertUtility.ToString(Portal[Definition.Portal_Contact_Mail + "_" + dropPortal.SelectedValue]);
                txtMailServer.Text = ConvertUtility.ToString(Portal[Definition.Portal_Mail_Server + "_" + dropPortal.SelectedValue]);
                txtMailUser.Text = ConvertUtility.ToString(Portal[Definition.Portal_Mail_User + "_" + dropPortal.SelectedValue]);
                txtMailPwd.Text = ConvertUtility.ToString(Portal[Definition.Portal_Mail_Password + "_" + dropPortal.SelectedValue]);
				//
                string sitename = ConvertUtility.ToString(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_SiteName + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage()]);
				txtSiteName.Text = sitename;
				txtCopyRight.Text = ConvertUtility.ToString(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_CopyRight + AppEnv.GetLanguage()]);                       
                
			}
		}

       

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            dropTabs.Items.Clear();
            dropSearchTab.Items.Clear();
            dropZoneTab.Items.Clear();
            dropContentTab.Items.Clear();
            dropPortalActive.Items.Clear();
            dropZoneActive.Items.Clear();
            
            dropEvent.Items.Clear();
            dropNewsInEvent.Items.Clear();
            dropAlbum.Items.Clear();
            dropAlbumDetail.Items.Clear();
            dropBonus.Items.Clear();
            dropBonusDetail.Items.Clear();
            
            PortalController.LoadPortals(dropPortalActive.Items);

            TabController.FillToList(dropTabs.Items, TabController.GetTabsByPortalID(ConvertUtility.ToInt32(dropPortal.SelectedValue)));
            dropTabs.Items.Insert(0, new ListItem("Select a tab", "0"));

            int homeTabId = ConvertUtility.ToInt32(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Home_Tab + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropTabs, homeTabId.ToString());

            int portalactive = ConvertUtility.ToInt32(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Active + "_" + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropPortalActive, portalactive.ToString());

            ZoneController.LoadZones(dropZoneActive.Items, dropPortal.SelectedValue);
            dropZoneActive.Items.Insert(0, new ListItem("Root", "0"));

            foreach (ListItem item in dropTabs.Items)
            {
                dropSearchTab.Items.Add(new ListItem(item.Text, item.Value));
                dropEvent.Items.Add(new ListItem(item.Text, item.Value));
                dropNewsInEvent.Items.Add(new ListItem(item.Text, item.Value));
                dropZoneTab.Items.Add(new ListItem(item.Text, item.Value));
                dropContentTab.Items.Add(new ListItem(item.Text, item.Value));

                dropAlbumDetail.Items.Add(new ListItem(item.Text, item.Value));
                dropAlbum.Items.Add(new ListItem(item.Text, item.Value));
                dropBonus.Items.Add(new ListItem(item.Text, item.Value));
                dropBonusDetail.Items.Add(new ListItem(item.Text, item.Value));
            }
            int zoneTabId = ConvertUtility.ToInt32(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Zone_Tab + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropZoneTab, zoneTabId.ToString());

            int zoneActive = ConvertUtility.ToInt32(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Zone_Active + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropZoneActive, zoneActive.ToString());

            int contentTabId = ConvertUtility.ToInt32(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Content_Tab + "_" + dropPortal.SelectedValue + "_"  +AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropContentTab, contentTabId.ToString());

            int searchTabId = ConvertUtility.ToInt32(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Search_Tab + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropSearchTab, searchTabId.ToString());

            int eventTabId = ConvertUtility.ToInt32(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Event_Tab + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropEvent, eventTabId.ToString());

            int newsineventTabId = ConvertUtility.ToInt32(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_NewsInEvent_Tab + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropNewsInEvent, newsineventTabId.ToString());

            int picListTabId = ConvertUtility.ToInt32(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_AlbumDetail_Tab + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropAlbumDetail, picListTabId.ToString());

            int picAlbumTabId = ConvertUtility.ToInt32(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Album_Tab + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropAlbum, picAlbumTabId.ToString());

            int bonusTabId = ConvertUtility.ToInt32(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Bonus_Tab + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropBonus, bonusTabId.ToString());

            int bonusDetailTabId = ConvertUtility.ToInt32(Framework.Library.Components.Portal.PortalSetting.GetSetting()[Definition.Portal_BonusDetail_Tab + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropBonusDetail, bonusDetailTabId.ToString());
            
            GetFolders(Server.MapPath("/"), Server.MapPath("/Style"));
            dropStyle.DataSource = lstFoder;
            dropStyle.DataBind();
            MiscUtility.SelectItemFromList(dropStyle, ConvertUtility.ToString(Portal[Definition.Portal_Style + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage()]));
        }
		protected void btnUpdate_Click(object sender, System.EventArgs e)
		{
			try
			{
                Framework.Library.Components.Portal.PortalSetting.SetValue(Definition.Portal_Home_Portal , dropPortal.SelectedValue);
				Framework.Library.Components.Portal.PortalSetting.SetValue(Definition.Portal_Home_Tab + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage(), dropTabs.SelectedValue);
                Framework.Library.Components.Portal.PortalSetting.SetValue(Definition.Portal_Zone_Tab + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage(), dropZoneTab.SelectedValue);
                Framework.Library.Components.Portal.PortalSetting.SetValue(Definition.Portal_Content_Tab + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage(), dropContentTab.SelectedValue);
                Framework.Library.Components.Portal.PortalSetting.SetValue(Definition.Portal_Search_Tab + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage(), dropSearchTab.SelectedValue);
                Framework.Library.Components.Portal.PortalSetting.SetValue(Definition.Portal_SiteName + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage(), txtSiteName.Text);

                Framework.Library.Components.Portal.PortalSetting.SetValue(Definition.Portal_Event_Tab + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage(), dropEvent.SelectedValue);
                Framework.Library.Components.Portal.PortalSetting.SetValue(Definition.Portal_NewsInEvent_Tab + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage(), dropNewsInEvent.SelectedValue);
                Framework.Library.Components.Portal.PortalSetting.SetValue(Definition.Portal_AlbumDetail_Tab + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage(), dropAlbumDetail.SelectedValue);
                Framework.Library.Components.Portal.PortalSetting.SetValue(Definition.Portal_Album_Tab + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage(), dropAlbum.SelectedValue);
                Framework.Library.Components.Portal.PortalSetting.SetValue(Definition.Portal_Bonus_Tab + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage(), dropBonus.SelectedValue);
                Framework.Library.Components.Portal.PortalSetting.SetValue(Definition.Portal_BonusDetail_Tab + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage(), dropBonusDetail.SelectedValue);
			    
                Framework.Library.Components.Portal.PortalSetting.SetValue(Definition.Portal_Contact_Mail + "_" + dropPortal.SelectedValue, txtContactMail.Text);
                Framework.Library.Components.Portal.PortalSetting.SetValue(Definition.Portal_Mail_Server + "_" + dropPortal.SelectedValue, txtMailServer.Text);
                Framework.Library.Components.Portal.PortalSetting.SetValue(Definition.Portal_Mail_User + "_" + dropPortal.SelectedValue, txtMailUser.Text);
				Framework.Library.Components.Portal.PortalSetting.SetValue(Definition.Portal_Mail_Password + "_" + dropPortal.SelectedValue, txtMailPwd.Text);
				Framework.Library.Components.Portal.PortalSetting.SetValue(Definition.Portal_CopyRight + AppEnv.GetLanguage(), txtCopyRight.Text);

                Framework.Library.Components.Portal.PortalSetting.SetValue(Definition.Portal_Style + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage(), dropStyle.SelectedValue);
                Framework.Library.Components.Portal.PortalSetting.SetValue(Definition.Portal_Active + "_" + AppEnv.GetLanguage(), dropPortalActive.SelectedValue);
                Framework.Library.Components.Portal.PortalSetting.SetValue(Definition.Portal_Zone_Active + "_" + dropPortal.SelectedValue + "_" + AppEnv.GetLanguage(), dropZoneActive.SelectedValue);
				lblUpdateStatus.Text= MiscUtility.MSG_UPDATE_SUCCESS;
			}
			catch(Exception ex)
			{
				Response.Write(ex.Message);
			}
		}
	}
}